//
//  DetailModel.m
//  Mango
//
//  Created by lanou3g on 16/5/9.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "DetailModel.h"

@implementation recommendModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}
- (void)setValue:(id)value forKey:(NSString *)key {
    [super setValue:value forKey:key];
    if ([key isEqualToString:@"id"]) {
        self.ID = [value integerValue];
    }
}

@end

@implementation DetailModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

- (void)setValue:(id)value forKey:(NSString *)key {
    [super setValue:value forKey:key];
    if ([key isEqualToString:@"footer"]) {
        self.recommend = value[@"recommend"];
    }
    NSMutableArray *array = [NSMutableArray new];
    for (NSDictionary *dict in self.recommend) {
        recommendModel *model = [[recommendModel alloc]init];
        [model setValuesForKeysWithDictionary:dict];
        [array addObject:model];
    }
    self.recommend = array;
    
    if ([key isEqualToString:@"info"]) {
        [self setValuesForKeysWithDictionary:value];
    }
   
    [self replaceContentWithImages];
 
    
}

- (void)replaceContentWithImages {
    NSArray *imgArray = self.resources[@"IMG"];
    for (int i = 0; i < imgArray.count; i++) {
        NSString *imgName = [NSString stringWithFormat:@"!--IMG#%d--",i];
        NSDictionary *imgInfoDict = imgArray[i];
        NSString *width = [NSString stringWithFormat:@"%ld",[imgInfoDict[@"width"]  integerValue]];
        NSString *height = [NSString stringWithFormat:@"%ld",[imgInfoDict[@"height"] integerValue]];
        NSString *imgStr = [imgInfoDict[@"src"] componentsSeparatedByString:@"!"][0];
        self.contents = [self.contents stringByReplacingOccurrencesOfString:imgName withString:[NSString stringWithFormat:@"img src='%@' width='%@' height='%@'",imgStr,width,height]].mutableCopy;
    }
}

@end
